package com.neusoft.nep.NEPG.controller;

import com.neusoft.nep.entity.Gridder;
import com.neusoft.nep.entity.nepsFeedback;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.VBox;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;

import static com.neusoft.tools.AlertUtils.showDialog;
import static com.neusoft.tools.FileUtils.readListObject;
import static com.neusoft.tools.FileUtils.readMapObject;


public class FeedbackListViewController implements Initializable {

    @FXML
    private VBox vBox;

    private Gridder gridder;

   private boolean dataLoaded = false;

    @Override
    public void initialize(URL url, ResourceBundle rb) {

    }

    public void initGridder(Gridder gridder) {
        this.gridder = gridder;
        if (!dataLoaded) {
            loadFeedbackCards();
            //防止重复加载
            dataLoaded = true;
        }
    }

    private void loadFeedbackCards() {
        String filename = "NEPGworks.txt";
        Map<String, List<nepsFeedback>> nepgfeedbackMap = readMapObject(filename);
        List<nepsFeedback> feedbackList =  nepgfeedbackMap.getOrDefault(gridder.getIdAsString(), new ArrayList<>());
        for (nepsFeedback feedback : feedbackList) {
            try {
                FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("../view/CardView.fxml"));
                AnchorPane cardView = fxmlLoader.load();
                CardViewController cardViewController = fxmlLoader.getController();
                cardViewController.setData(feedback, gridder);
                vBox.getChildren().add(cardView);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        if (feedbackList.isEmpty()) showDialog("","当前没有指派的任务。");
    }
}